<?php 
/**
* @orm tableName categories
*/
class Category extends DomArObject {
	/**
	* @orm text
	*/
	public $name;
	/**
	* @orm owns many Category inverse parentCategory
	*/
	public $categories;
	/**
	* @orm has parent Category inverse categories
	*/
	public $parentCategory;
	/**
	* @orm owns many Article inverse category
	*/
	public $articles;
	
	public function getCategoryTree( &$tree = array(), $level = 0 ) {
		
		$tree[] = array('id'=>$this->id, 'value' => $this->name, 'level' => $level, 'category' => $this );
		foreach( $this->categories as $category ) {
			$category->getCategoryTree( $tree, $level+1 );
		}
		return $tree;
	}
	
	public function getFullCategoryTree( $addNullElement = false ) {
		$tree = array();
		if( $addNullElement )
			$tree[] = array('id'=>0, 'value' => 'Pole', 'level' => 0, 'category' => NULL );
			
		$roots = Category::query()->where('parentCategory <=> NULL')->orderBy('t.name')->commit();
		
		foreach( $roots as $root ) {
			$root->getCategoryTree( $tree, 0 );
		}	
		return $tree;
	}
}
?>